package com.yiwei.ydd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.WalletDetailAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.PageBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyWalletActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_switch)
    ImageView imgSwitch;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.btn_recharge)
    LinearLayout btnRecharge;
    @BindView(R.id.btn_withdraw)
    LinearLayout btnWithdraw;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;

    private Unbinder unbiner;
    private WalletDetailAdapter adapter;
    private boolean isOpen = true;
    private boolean isOkRefresh = false;
    private int currentPage = 1;
    private String money;
    private String apply_fee;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {

        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY == (scrollView.getChildAt(0).getMeasuredHeight() - scrollView.getMeasuredHeight())) {
                    //底部加载
                    if (isOkRefresh) {
                        isOkRefresh = false;
                        currentPage++;
                        getMoneyLists(currentPage);
                    }
                }
            }
        });
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getMoneyLists(1);
            }
        });
        txtTitle.setText("我的钱包");
        adapter = new WalletDetailAdapter(this);
        adapter.setOnChooseListener(new WalletDetailAdapter.OnChooseListener() {
            @Override
            public void onClick(String order_type, String order_no) {
//                （1：充值油卡套餐，2：用户升级，3：余额充值，4：油卡代办订单 9：客服扣除 10：提现）
                if (order_type.equals("10")) {
                    Intent intent = new Intent(MyWalletActivity.this, WithdrawDetailActivity.class);
                    intent.putExtra("order_no", order_no);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                } else if (order_type.equals("3")) {
//                    余额充值
                    Intent intent = new Intent(MyWalletActivity.this, BagRechargeDetailActivity.class);
                    intent.putExtra("order_no", order_no);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                } else if (order_type.equals("1")) {
//                    充值油卡套餐
                    Intent intent = new Intent(MyWalletActivity.this, MyOrderDetailActivity.class);
                    intent.putExtra("order_number", order_no);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                } else if (order_type.equals("2")) {
//                    用户升级
                    Intent intent = new Intent(MyWalletActivity.this, VipOrderDetailActivity.class);
                    intent.putExtra("order_no", order_no);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                } else if (order_type.equals("4")) {
//                    油卡代办订单
                    Intent intent = new Intent(MyWalletActivity.this, AgencyRecordActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                } else if (order_type.equals("9")) {
//                    客服扣除
                    Intent intent = new Intent(MyWalletActivity.this, DaiKouDetailActivity.class);
                    intent.putExtra("order_no", order_no);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                }else if (order_type.equals("11")) {
//                    客服扣除
                    Intent intent = new Intent(MyWalletActivity.this, MyCommissionDetailActivity.class);
//                    intent.putExtra("order_no", order_no);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                }
            }
        });
        list.setNestedScrollingEnabled(false);
        list.setAdapter(adapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        getMoneyIndex();
        getMoneyLists(1);
    }

    private void getMoneyIndex() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        Api.api_service.getMoneyIndex().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    txtMoney.setText(result.datas.money);
                    money = result.datas.money;
                    apply_fee = result.datas.apply_fee;
                });
    }

    private void getMoneyLists(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        PageBean bean = new PageBean();
        bean.page = page;
        Api.api_service.getMoneyLists(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                        isOkRefresh = true;
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.txt_right, R.id.btn_back, R.id.img_switch, R.id.btn_recharge, R.id.btn_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_right:
                Util.startActivity(this, BankListActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.img_switch:
                isOpen = !isOpen;
                if (isOpen) {
                    imgSwitch.setImageResource(R.mipmap.img_eye_open);
                } else {
                    imgSwitch.setImageResource(R.mipmap.img_eye_close);
                }

                txtMoney.setInputType(isOpen ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                break;
            case R.id.btn_recharge:
                Intent intent = new Intent(this, WalletRechargeActivity.class);
                intent.putExtra("money", money);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case R.id.btn_withdraw:
                Intent intent1 = new Intent(this, WalletWithdrawActivity.class);
                intent1.putExtra("money", money);
                intent1.putExtra("apply_fee", apply_fee);
                startActivity(intent1);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }
}
